package org.fig.dependency.injection;

import org.fig.ioc.overview.domain.User;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @Description: 延迟注入
 * @Author: Flynn Vanderboom
 * @Data: 2021/6/20 22:18
 */

public class LazyAnnotationDependencyInjectionDemo {


    @Autowired
    private User user;

    @Autowired
    private ObjectProvider<User> usersObjectProvider;

    @Autowired
    private ObjectFactory<Set<User>> usersObjectFactory;

    public static void main(String[] args) {
        //创建BeanFactory容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        //注册  配置类   ---->Spring Bean
        context.register(LazyAnnotationDependencyInjectionDemo.class);


        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(context);

        String xmlResourcePath = "classpath:META-INF/dependency-lookup-context.xml";

        //加载XML资源，解析并且生成BeanDefinition
        reader.loadBeanDefinitions(xmlResourcePath);


        //启动上下文
        context.refresh();

        //依赖查找 AnnotationDependencyFieldInjectionDemo
        LazyAnnotationDependencyInjectionDemo demo = context.getBean(LazyAnnotationDependencyInjectionDemo.class);

        System.out.println("demo.user == "+ demo.user);
        System.out.println("demo.usersObjectProvider == "+demo.usersObjectProvider.getObject());
        System.out.println("demo.usersObjectFactory == "+demo.usersObjectFactory.getObject());
        demo.usersObjectProvider.forEach(System.out::println);


        //关闭上下文
        context.close();
    }





}
